package com.jp.vn.CustomAdapter;

import java.util.ArrayList;
import java.util.Locale;

import com.jp.vn.main.R;
import com.jp.vn.Object.ThoiGian;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.speech.tts.TextToSpeech;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ViDuAdapter extends BaseAdapter implements TextToSpeech.OnInitListener {

	ArrayList<ThoiGian> arrayThoiGian;
	Context context;
	private TextToSpeech tts;
	LayoutInflater inflater;
	
	@Override
	public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS) {
			tts.setLanguage(Locale.JAPANESE);
		}
	}
	
	public ViDuAdapter(Context context, ArrayList<ThoiGian> arrayThoiGian) {
		this.context = context;
		this.arrayThoiGian = arrayThoiGian;
		tts = new TextToSpeech(context, this);
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);	
	}

	@Override
	public int getCount() {
		return arrayThoiGian.size();
	}

	@Override
	public Object getItem(int position) {
		return arrayThoiGian.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public class ViewHolder {

		TextView tfTimeJp, tfTimeVn;
		ImageView btnAudio;
		LinearLayout layout_tg;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		Typeface type = Typeface.createFromAsset(context.getAssets(),"fonts/tekton.ttf");

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_tg, null);
			
			holder.tfTimeVn = (TextView)convertView.findViewById(R.id.tfThoiGian);
			holder.tfTimeJp = (TextView)convertView.findViewById(R.id.tfJp_Time);
			holder.btnAudio = (ImageView)convertView.findViewById(R.id.imgAudio);
			holder.layout_tg = (LinearLayout)convertView.findViewById(R.id.layout_tg);
			
			convertView.setTag(holder);
		}

		else {
			holder = (ViewHolder) convertView.getTag();
		}
		
			ThoiGian time  =  arrayThoiGian.get(position);
			holder.tfTimeVn.setText(time.getTimeVn());
			holder.tfTimeJp.setText(time.getTimeJp());
			
			holder.tfTimeVn.setTypeface(type,Typeface.BOLD_ITALIC);
			holder.tfTimeJp.setTypeface(type,Typeface.BOLD_ITALIC);
			
			holder.layout_tg.setMinimumHeight((int) context.getResources().getDimension(R.dimen.item_height));
			holder.layout_tg.setBackgroundColor(position % 2 == 0 ? Color.parseColor("#fdbed1") : Color.parseColor("#fbe0e8"));
			
			holder.btnAudio.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					String text = holder.tfTimeJp.getText().toString();
					tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
				}
			});

		return convertView;
	}
}
